<template>
  <div class="masonry-grid">
    <div 
      v-for="(image, index) in images" 
      :key="index"
      class="grid-item"
      @click="handleImageClick(image)"
    >
      <div class="image-wrapper">
        <img :src="image.src" :alt="image.title">
      </div>
      <div class="image-info">
        <h3 class="image-title">{{ image.title }}</h3>
        <div class="author-info">
          <div v-if="image.author !== '300分享分'" class="author-avatar">
            <img :src="avatarIcon" alt="头像">
          </div>
          <div class="author-details">
            <template v-if="image.author === '300分享分'">
              <span class="share-points">
                <img :src="coinIcon" alt="金币" class="coin-icon">
                300分享分
              </span>
            </template>
            <template v-else>
              <div class="author-name-row">
                <span class="author-name">{{ image.author }}</span>
              </div>
              <div class="author-meta-row">
                <span class="author-level">Lv.4</span>
                <span class="author-role">资深设计师</span>
              </div>
            </template>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'ImageGrid',
  props: {
    images: {
      type: Array,
      required: true
    }
  },
  data() {
    return {
      avatarIcon: require('~/assets/images/img/B-01首页_slices/头像.png'),
      coinIcon: require('~/assets/images/img/B-01首页_slices/金币.png')
    }
  },
  methods: {
    handleImageClick(image) {
      // 使用作品的 id，如果没有则使用索引
      const workId = image.id || (this.images.indexOf(image) + 1).toString()
      this.$router.push(`/works/${workId}`)
    }
  }
}
</script>

<style scoped>
.masonry-grid {
  column-count: 6;
  column-gap: 20px;
}

@media (min-width: 1920px) {
  .masonry-grid {
    column-count: 6;
  }
}

@media (max-width: 1600px) {
  .masonry-grid {
    column-count: 5;
  }
}

@media (max-width: 1200px) {
  .masonry-grid {
    column-count: 4;
  }
}

@media (max-width: 768px) {
  .masonry-grid {
    column-count: 3;
  }
}

@media (max-width: 480px) {
  .masonry-grid {
    column-count: 2;
  }
}

.grid-item {
  break-inside: avoid;
  margin-bottom: 25px;
  background: white;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.3s, box-shadow 0.3s;
  display: flex;
  flex-direction: column;
}

.grid-item:hover {
  box-shadow: 0 10px 30px rgba(0,0,0,0.15);
}

.image-wrapper {
  width: 100%;
  overflow: hidden;
}

.grid-item img {
  width: 100%;
  height: auto;
  display: block;
  object-fit: cover;
}

.image-info {
  padding: 15px;
  background: white;
}

.image-title {
  font-size: 14px;
  color: #333;
  margin-bottom: 15px;
  font-weight: 500;
  line-height: 1.4;
}

.author-info {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  min-height: 32px;
}

.author-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  flex-shrink: 0;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}

.author-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}


.author-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.author-name-row {
  display: flex;
  align-items: center;
}

.author-name {
  font-size: 12px;
  color: #333;
  font-weight: 500;
}

.author-meta-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.author-level {
  font-size: 12px;
  color: #19D8B6;
  font-weight: 500;
}

.author-role {
  font-size: 12px;
  color: #999;
}

.share-points {
  font-size: 12px;
  color: #333;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 4px;
}

.coin-icon {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: block;
  max-width: 18px;
  max-height: 18px;
  object-fit: contain;
}
</style>

